package com.yswee.framework.net.http;

import java.net.HttpURLConnection;

import com.yswee.framework.model.PageType;
import com.yswee.framework.net.ICallback;

public abstract class Callback<TPARSED> implements ICallback<Entity<TPARSED>>, Cloneable {

	private PageType mPageType = PageType.FirstPage;
	
	public void setPageType(PageType type) {
		mPageType = type;
	}
	
	public PageType getPageType() {
		return mPageType;
	}
	
	@Override
	public abstract void onFinish(Entity<TPARSED> entity);

	@Override
	public abstract void onError(Entity<TPARSED> entity);

	@Override
	public void onCancel(Entity<TPARSED> entity) { }
	
	@Override
	public void onInvalid(Entity<TPARSED> entity) { }

	@Override
	public void onBeginResponse(HttpURLConnection connection) { }

	@Override
	public void onEndResponse(Entity<TPARSED> entity) { }

	@Override
	public void onBeginRequest(Entity<TPARSED> entity) { }
	
	@Override
	public void onBeginConnection(HttpURLConnection connection) { }
	
	/**
	 * 网络请求下载进度（UI线程）
	 * @param entity
	 */
	public void onDownloadProgress(Entity<TPARSED> entity) { }
	
	/**
	 * 网络上传进度（UI线程）
	 * @param entity
	 */
	public void onUploadProgress(Entity<TPARSED> entity) { }
	
	/**
	 * 下载数据后，解析数据（非UI线程）
	 * @param result
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public TPARSED onProcessData(Entity<TPARSED> entity, String result) {
		if(entity.getParser() != null) {
			return entity.getParser().parseData(result);
		} else {
			return (TPARSED)result;
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Callback<TPARSED> clone() {
		Object o = null;
		try {
			o = super.clone();
		} catch (CloneNotSupportedException e) {
			System.out.println(e.toString());
		}
		return (Callback<TPARSED>) o;
	}
}
